//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by 石林 on 15/11/18.
//  Copyright © 2015年 石林. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let pageCount = 4
class NewFeatureViewController: UICollectionViewController {
    
    
    init() {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = screenSize
//        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
//        collectionView?.addSubview(startBtn)
//        startBtn.hidden = true
//        startBtn.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo((collectionView?.snp_centerX)!).offset(screenWidth * 3)
//            make.centerY.equalTo((collectionView?.snp_centerY)!).offset(screenHeight * 0.25)
//        }
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //开始体验布局
        collectionView?.addSubview(startBtn)
        startBtn.hidden = true
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo((collectionView?.snp_centerX)!).offset(screenWidth * 3)
            make.centerY.equalTo((collectionView?.snp_centerY)!).offset(screenHeight * 0.25)
        }
        
        startBtn.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenWidth)
        if page == pageCount - 1 {
            
            startAnimation()
        }else {
            startBtn.hidden = true
        }
    }
    
    private func startAnimation() {
        
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("startanimation")
        }
    }
    
    @objc private func start() {
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    
    private lazy var startBtn: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: UIColor.whiteColor())
//    {
//        let btn = UIButton()
//        btn.backgroundColor = UIColor.orangeColor()
//        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
//        btn.setTitle("开始体验", forState: .Normal)
//        
//        return btn
//    }()


    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.orangeColor() : UIColor.grayColor()
        cell.index = indexPath.item
    
        return cell
    }

}

class NewFeatureCell:UICollectionViewCell {
    
    var index: Int = 0 {
        
        didSet {
        
            newFeatureImage.image = UIImage(named: "new_feature_\(index + 1)")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        contentView.addSubview(newFeatureImage)
        
        newFeatureImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
    }
    
    private lazy var newFeatureImage: UIImageView = UIImageView()
    
}
